import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
	
public class Game extends JFrame implements ActionListener
{
	private JButton betButton, dealButton, playAgain;
	private JButton[] buttonArray = new JButton[5];
	private JPanel middlePanel, topPanel, bottomPanel, buttonPanel, optPanel;
	protected static JLabel bankLabel, nameLabel, betLabel;
	private JLabel[] board;
	protected static JRadioButton[] radioButton;
	private ButtonGroup radioButtons = new ButtonGroup();
	private JLabel textLabel;
	private Player player = new Player();
	private Deck deck;
	private boolean[] isDiscard = new boolean[5];
	private ButtonGroup group = new ButtonGroup();
	private int betAmount, win;
	private Card[] hand;
	
	public void setName(String string)
	{
		if(string == null || string.equals(""))
			nameLabel.setText("Name: Player");
		else {
			nameLabel.setText("Name: "+ string);
			player.playerName = string;
		}
	}
	
	public void setGame(String string)
	{
		try {
			player.loadGame(string);
		}
		catch(java.util.NoSuchElementException e) {
			JOptionPane.showMessageDialog(null, "Name wasn't in the save file.  Defaulting to guest player(new game)", "Error", JOptionPane.WARNING_MESSAGE);
			nameLabel.setText("Name: " + string);
			bankLabel.setText("Bank: $1000");
		}
	}

	public Game()
	{
		deck = new Deck();
		player = new Player();
		//Set all the panels
		topPanel = new JPanel(new GridLayout(1,3));
		middlePanel = new JPanel(new GridLayout(2,2));
		bottomPanel = new JPanel(new GridLayout(2,1));
		optPanel = new JPanel(new GridLayout(1,2));
		
		//Set and add labels to the top panel
		bankLabel = new JLabel("Bank: $" + player.bank, JLabel.CENTER);
		bankLabel.setBorder(BorderFactory.createLineBorder(Color.black));
		bankLabel.setFont(new Font("Arial", Font.BOLD, 20));
		betLabel = new JLabel("Bet Amount: $" + player.betAmount, JLabel.CENTER);
		betLabel.setBorder(BorderFactory.createLineBorder(Color.black));
		betLabel.setFont(new Font("Arial", Font.BOLD, 20));
		nameLabel = new JLabel("Name: ", JLabel.CENTER);
		nameLabel.setBorder(BorderFactory.createLineBorder(Color.black));
		nameLabel.setFont(new Font("Arial", Font.BOLD, 20));
		
		topPanel.add(bankLabel);
		topPanel.add(betLabel);
		topPanel.add(nameLabel);
		
		//create the deal button
		dealButton = new JButton("Deal");
		dealButton.setEnabled(false);
		dealButton.addActionListener(this);
		
		//Create the instruction label at the bottom and add it
		textLabel = new JLabel("Place bet!", JLabel.CENTER);
		textLabel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
		textLabel.setFont(new Font("Arial", Font.BOLD, 20));
		playAgain = new JButton("Play Again!");
		playAgain.setEnabled(false);
		playAgain.addActionListener(this);
		bottomPanel.add(dealButton);
		optPanel.add(textLabel);
		optPanel.add(playAgain);
		bottomPanel.add(optPanel);
		
		//Create the JLabel array to hold the images
		board = new JLabel[5];
		
		//Fill the array with the default card image and initialize the discard boolean array
		for(int i=0; i<5; i++)
		{
				JLabel label = new JLabel(new ImageIcon("DEFAULT.gif"));
				board[i] = label;
				middlePanel.add(board[i]);
				isDiscard[i] = false;
				
		}
		
		//Create and add all of the radio buttons, add them to the button group
		radioButton = new JRadioButton[8];
		buttonPanel = new JPanel(new GridLayout(8,1));
		
		JRadioButton bet1 = new JRadioButton("$10");
		bet1.addActionListener(this);
		group.add(bet1);
		radioButton[0] = bet1;
		
		JRadioButton bet2 = new JRadioButton("$25");
		bet2.addActionListener(this);
		group.add(bet2);
		radioButton[1] = bet2;
		
		JRadioButton bet3 = new JRadioButton("$50");
		bet3.addActionListener(this);
		group.add(bet3);
		radioButton[2] = bet3;
		
		JRadioButton bet4 = new JRadioButton("$100");
		bet4.addActionListener(this);
		group.add(bet4);
		radioButton[3] = bet4;
		
		JRadioButton bet5 = new JRadioButton("$150");
		bet5.addActionListener(this);
		group.add(bet5);
		radioButton[4] = bet5;
		
		JRadioButton bet6 = new JRadioButton("$200");
		bet6.addActionListener(this);
		group.add(bet6);
		radioButton[5] = bet6;
		
		JRadioButton bet7 = new JRadioButton("$250");
		bet7.addActionListener(this);
		group.add(bet7);
		radioButton[6] = bet7;
		
		JRadioButton bet8 = new JRadioButton("$500");
		bet8.addActionListener(this);
		group.add(bet8);
		radioButton[7] = bet8;
		
		for(int i=0; i<8; i++)
		{
			buttonPanel.add(radioButton[i]);
			radioButtons.add(radioButton[i]);
		}
		middlePanel.add(buttonPanel);
		
		//create the discard buttons and initialize them to 'discard'
		for(int i=0; i<5; i++)
		{
			
			buttonArray[i] = new JButton("Discard");
			buttonArray[i].setEnabled(false);
			buttonArray[i].addActionListener(this);
			middlePanel.add(buttonArray[i]);
			
		}
		betButton = new JButton ("Place Bet!");
		betButton.addActionListener(this);
		betButton.setEnabled(false);
		middlePanel.add(betButton);
		
		setLayout(new GridLayout(3,1));
		add(topPanel);
		add(middlePanel);
		add(bottomPanel);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		for(int i=0; i<8; i++)
		{
			if(event.getSource() == radioButton[i])
			{
				betButton.setEnabled(true);
				betAmount = i;
			}
		}
		
		if(event.getSource() == betButton)
		{
			player.setHand(deck);
			hand = player.getHand();
			Card tempCard;
			for(int i=0; i<5; i++)
			{
				tempCard = player.getCard(i);
				String string = tempCard.toString();
				ImageIcon icon = new ImageIcon(string  + ".gif");
				board[i].setIcon(icon);
				buttonArray[i].setEnabled(true);
			}
			betButton.setEnabled(false);
			dealButton.setEnabled(true);
			playAgain.setEnabled(false);
			textLabel.setText("Choose which cards to discard and press DEAL.");
			textLabel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
			
			if(betAmount == 0)
				betAmount = 10;
			else if(betAmount == 1)
				betAmount = 25;
			else if(betAmount == 2)
				betAmount = 50;
			else if(betAmount == 3)
				betAmount = 100;
			else if(betAmount == 4)
				betAmount = 150;
			else if(betAmount == 5)
				betAmount = 200;
			else if(betAmount == 6)
				betAmount = 250;
			else if(betAmount == 7)
				betAmount = 500;
				
			betLabel.setText("Bet Amount: $" + betAmount);
			player.bank = player.bank - betAmount;
			bankLabel.setText("Bank: $" + player.bank);
			for(int i=0; i<8; i++)
				radioButton[i].setEnabled(false);
			radioButtons.clearSelection();
			
		}
		for(int i=0; i<5; i++)
		{
			if(event.getSource() == buttonArray[i] && buttonArray[i].getText() == "Discard")
			{
				board[i].setBorder(BorderFactory.createLineBorder(Color.red,5));
				buttonArray[i].setText("Keep");
				isDiscard[i] = true;
			}
			else if(event.getSource() == buttonArray[i] && buttonArray[i].getText() == "Keep")
			{
				board[i].setBorder(null);
				buttonArray[i].setText("Discard");
				isDiscard[i] = false;
			}
		}
		if(event.getSource() == dealButton)
		{
			Card newCard;
			for (int i=0; i<5; i++)
			{
				if(isDiscard[i])
				{
					newCard = deck.getNewCard();
					hand[i] = newCard;
					String string = newCard.toString();
					ImageIcon icon = new ImageIcon(string  + ".gif");
					board[i].setIcon(icon);
					board[i].setBorder(null);
					buttonArray[i].setText("Discard");
					isDiscard[i] = false;	
				}
			}
			
				win  = player.checkWin();
				String winString;
				switch(win)
				{
					case 1:
						winString = "a royal flush!";
						player.bank = player.bank + (250*betAmount);
						bankLabel.setText("Bank: $" + player.bank);
						break;
					case 2: 
						winString = "a straight flush.";
						player.bank = player.bank + (50*betAmount);
						bankLabel.setText("Bank: $" + player.bank);
						break;
					case 3:
						winString = "a straight.";
						player.bank = player.bank + (25*betAmount);
						bankLabel.setText("Bank: $" + player.bank);
						break;
					case 4:
						winString = "a flush.";
						player.bank = player.bank + (9*betAmount);
						bankLabel.setText("Bank: $" + player.bank);
						break;
					case 5:
						winString = "a full house.";
						player.bank = player.bank + (6*betAmount);
						bankLabel.setText("Bank: $" + player.bank);
						break;
					case 6:
						winString = "four of a kind.";
						player.bank = player.bank + (4*betAmount);
						bankLabel.setText("Bank: $" + player.bank);
						break;
					case 7:
						winString = "three of a kind.";
						player.bank = player.bank + (3*betAmount);
						bankLabel.setText("Bank: $" + player.bank);
						break;
					case 8:
						winString = "two pair.";
						player.bank = player.bank + (2*betAmount);
						bankLabel.setText("Bank: $" + player.bank);
						break;
					case 9:
						winString = "a pair.";
						player.bank = player.bank + (1*betAmount);
						bankLabel.setText("Bank: $" + player.bank);
						break;
					default:
						winString = "";
				}
				
				if(win == 0)
				{
					textLabel.setText("You Lose!");
					player.numLoss ++;
				}
				else 
				{
					textLabel.setText("You win with " + winString);
					player.numWins ++;
				}
				dealButton.setEnabled(false);
				playAgain.setEnabled(true);
				for(int i = 0; i < 5; i++)
					buttonArray[i].setEnabled(false);
				player.saveGame(player.playerName);
		}
		
		if(event.getSource() == playAgain)
		{
			for(int i = 0; i < 5; i++)
				board[i].setIcon(new ImageIcon("DEFAULT.gif"));
			textLabel.setText("Place bet!");
			textLabel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
			deck = new Deck();
			player.loadGame(player.playerName);
			player.setHand(deck);
			hand = player.getHand();
			playAgain.setEnabled(false);
			betLabel.setText("Bet: $0");
		}
	}
	
	public static void main(String[] args)
	{
		Game window = new Game();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		window.setSize(screenSize.width-4, screenSize.height-50);
		window.setTitle("Five-Card Poker");
		window.validate();
		window.setVisible(true);
		int n = JOptionPane.showConfirmDialog(null,"Would you like to load a game?","Load option", JOptionPane.YES_NO_OPTION);
		String x = (String)JOptionPane.showInputDialog(null, "Enter your name!", "Name Selection", JOptionPane.INFORMATION_MESSAGE, null, null, null);
		if(x == null || x.equals("")) x = "Player";
		if(n == 0)
		{
			try {window.setGame(x);}
			catch(java.util.NoSuchElementException e) {
				window.setName(x);
			}
		}
		else
		{
			window.setName(x);
		}
	}
}